<?php

namespace Users\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use ZendSearch\Lucene;
use ZendSearch\Lucene\Document;
//use ZendSearch\Lucene\Index;

use  Zend\Stdlib\ArrayUtils;

use Traversable;

/**
 * SearchController
 *
 * @author
 *
 * @version
 *
 */
class SearchController extends AbstractActionController {
	/**
	 * The default action - show the home page
	 */
	public function indexAction()
    { 
	$request = $this->getRequest();
	$searchResults=Null;
	if ($request->isPost()) {
		$queryText = $request->getPost()->get('query');
		$searchIndexLocation =  $this->getIndexLocation();
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d '.var_dump($searchIndexLocation));
		$index = Lucene\Lucene::open($searchIndexLocation);
		$searchResults = $index->find($queryText);
	}
	// prepare search form
	$form = new \Zend\Form\Form();
	$form->add(array(
		'name' => 'query',
		'attributes' => array(
		'type' => 'text',
		'id' => 'queryText',
		'required' => 'required'
		),
		'options' => array('label' => 'Search String',
		),
	));
	$form->add(array(
		'name' => 'submit',
		'attributes' => array(
		'type' => 'submit',
		'value' => 'Search'
		),
	));
	$viewModel = new ViewModel(array('form' => $form, 'searchResults' => $searchResults));
	
	return $viewModel;
    }
	
    public function index2Action()
    {
	$request = $this->getRequest();
	if ($request->isPost()) {
		$queryText = $request->getPost()->get('query');
		$searchIndexLocation =  $this->getIndexLocation();
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d '.var_dump($searchIndexLocation));
		$index = Lucene\Lucene::open($searchIndexLocation);
		$searchResults = $index->find($queryText);
	}
	
	$viewModel = new ViewModel(array('searchResults' => $searchResults));

	return $viewModel;
    }
    
	public function getIndexLocation()
	{
		// Fetch Configuration from Module Config
		$config = $this->getServiceLocator()->get('config');
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d '.var_dump($config['module_config']['search_index']));
		if ($config instanceof Traversable) {
			$config = ArrayUtils::iteratorToArray($config);
		}
		if (!empty($config['module_config']['search_index'])) {
			return $config['module_config']['search_index'];
		} else {
			return FALSE;
		}
	}
	
	public function generateIndexAction()
	{
		$searchIndexLocation = $this->getIndexLocation();
		$index = \ZendSearch\Lucene\Lucene::create($searchIndexLocation);
		$userTable = $this->getServiceLocator()->get('Users\Model\UserTable');
		$uploadTable = $this->getServiceLocator()->get('UploadTable');
		$allUploads = $uploadTable->fetchAll();
		//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d '.var_dump($allUploads));
		foreach($allUploads as $fileUpload) {
			//
			$uploadOwner = $userTable->getUser($fileUpload->user_id);
			// create lucene fields
			$fileUploadId = Document\Field::unIndexed(
					'upload_id', $fileUpload->id);
			$label = Document\Field::Text(
					'label', $fileUpload->label);
			$owner = Document\Field::Text(
					'owner', $uploadOwner->name);
			// create a new document and add all fields
			// $indexDoc = new Lucene\Document(); //universal atempt
			
			if (substr_compare($fileUpload->filename, ".xlsx",
					strlen($fileUpload->filename) - strlen(".xlsx"),
					strlen(".xlsx")) === 0) {
						// index excel sheet
						$uploadPath = $this->getFileUploadLocation();
						//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d '.var_dump($uploadPath));
						$indexDoc = Lucene\Document\Xlsx::loadXlsxFile(
								$uploadPath ."/" . $fileUpload->filename);
					} else if (substr_compare($fileUpload->filename, ".docx",
							strlen($fileUpload->filename) - strlen(".docx"),
							strlen(".docx")) === 0) {
								// index word doc
								$uploadPath= $this->getFileUploadLocation();
								//throw new \Zend\File\Transfer\Exception\InvalidArgumentException('b��d '.var_dump($uploadPath));
								$indexDoc = Lucene\Document\Docx::loadDocxFile(
										$uploadPath ."/" . $fileUpload->filename);
							} else {
								$indexDoc = new Lucene\Document();
							}
			
			$indexDoc->addField($label);
			$indexDoc->addField($owner);
			$indexDoc->addField($fileUploadId);
			$index->addDocument($indexDoc);
		}
		$index->commit();
		
		$viewModel = new ViewModel();
		
		return $viewModel;
	}
	
	public function getFileUploadLocation()
	{
		// Fetch Configuration from Module Config
	
		$config = $this->getServiceLocator()->get('config');
		return $config['module_config']['upload_location'];
	}
}